﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;
using System.Drawing;
using GamePlugin.Logics.Data;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionRunSelectEquip : MachineState
    {
        public override void Initialize()
        {
            this.AddState("MoveMouse");
            this["MoveMouse"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.MoveMouse(o, e));

            this.AddState("SelectNow");
            this["SelectNow"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.SelectNow(o, e));

            this.AddState("Finished");
            this["Finished"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.Finished(o, e));

            this.DefaultState = "MoveMouse";
        }

        public void MoveMouse(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            Helper.MoveCursor(ActionData.EquitPoint);

            if (Workspace.Instance.SearchItemCompare.EquipKindChanged == false)
                this.ChangeToState("Finished");
            else
                this.ChangeToState("SelectNow");
        }

        public void SelectNow(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            int count = Workspace.Instance.SearchItem.EquipKind;
            Helper.MoveCursor(ActionData.OccupationEquipItemX, (int)(ActionData.EquitStartY + ActionData.OccupationEquipItemHeight * (count + 0.5f)));
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            ChangeToState("Finished");
        }

        public void Finished(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            this.IsNeedReset = true;
            this.Owner.ChangeToState(typeof(DActionRunSelectItem).Name);
        }
    }
}
